package com.application.tourapp;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.application.tourapp.async.TypesAsyncActivity;
import com.application.tourapp.baseadapter.TypeListBaseAdapter;
import com.application.tourapp.dal.TypeDAL;
import com.application.tourapp.global.VaraibleGlobal;
import com.application.tourapp.model.Type;

/**
 * Cette activite est pour les types
 * 
 * @author Ramy
 * 
 */

public class TypesActivity extends GenericActivity {

	
	private Integer[] imgid = { R.drawable.unkown };

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.types_activity);
		setHeader(getResources().getString(R.string.Types), true, true);
		Context context = getApplicationContext();
		new TypesAsyncActivity(this).execute();
		
		final Button buttonRecherche = (Button) findViewById(R.id.btnRechercher);
		buttonRecherche.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				 Intent intent = new Intent(getApplicationContext(), RechercheActivity.class);
				 startActivity(intent);	
				startActivity(intent);

			}
		});
		
		
		ArrayList<Type> typeDetails = GetSearchResults(context);

		final HashMap<Integer, Type> hashMapType = new HashMap<Integer, Type>();
		for (int i = 0; i < typeDetails.size(); i++) {
			hashMapType.put(i, typeDetails.get(i));
		}



		final TextView textField = (TextView) findViewById(R.id.textFieldIndice);
		final ImageView imageView = (ImageView) findViewById(R.id.imageButton1);

		if (hashMapType.get(VaraibleGlobal.getIndice()).getImageBlob() == null) {
			imageView.setImageResource(imgid[0]);
		} else {
			byte[] blob = hashMapType.get(VaraibleGlobal.getIndice())
					.getImageBlob();
			Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0, blob.length);
			if (bmp != null) {
				imageView.setImageBitmap(bmp);
			} else {
				imageView.setImageResource(imgid[0]);
			}
		}

		imageView.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {

				Intent touristicItemsScreenIntent = new Intent(
						getApplicationContext(), TouristicItemsActivity.class);
				ItemsGroupActivity parentActivity = (ItemsGroupActivity) getParent();
				touristicItemsScreenIntent.putExtra(
						"id_type",
						Integer.toString(hashMapType.get(
								VaraibleGlobal.getIndice()).getId()));
				parentActivity.startChildActivity("TouristicItemsActivity",
						touristicItemsScreenIntent);

			}
		});

		textField.setBackgroundColor(Color.BLACK);
		textField.setTextColor(Color.WHITE);
		textField
				.setText(String.valueOf(VaraibleGlobal.getIndice()));

		Button btnPlus = (Button) findViewById(R.id.buttonPlus);
		btnPlus.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				
				if (VaraibleGlobal.getIndice() < hashMapType.size()-1) {
					VaraibleGlobal.ajoutIndice();

					textField.setText(String.valueOf(VaraibleGlobal.getIndice()));

					if (hashMapType.get(VaraibleGlobal.getIndice())
							.getImageBlob() == null) {
						imageView.setImageResource(imgid[0]);
					} else {
						byte[] blob = hashMapType.get(
								VaraibleGlobal.getIndice()).getImageBlob();
						Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0,
								blob.length);
						if (bmp != null) {
							imageView.setImageBitmap(bmp);
						} else {
							imageView.setImageResource(imgid[0]);
						}
					}
				}

			}
		});

		Button btnMoins = (Button) findViewById(R.id.buttonMoins);

		btnMoins.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				if (VaraibleGlobal.getIndice() != 0) {

					VaraibleGlobal.retirerIndice();

					textField.setText(String.valueOf(VaraibleGlobal.getIndice()));

					if (hashMapType.get(VaraibleGlobal.getIndice())
							.getImageBlob() == null) {
						imageView.setImageResource(imgid[0]);
					} else {
						byte[] blob = hashMapType.get(
								VaraibleGlobal.getIndice()).getImageBlob();
						Bitmap bmp = BitmapFactory.decodeByteArray(blob, 0,
								blob.length);
						if (bmp != null) {
							imageView.setImageBitmap(bmp);
						} else {
							imageView.setImageResource(imgid[0]);
						}
					}
				}

			}

		});

	}

	public ArrayList<Type> GetSearchResults(Context context) {
		return new TypeDAL(context).getAlltypes();
	}
}